package ltd.dujiabao.reserver.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author : DuJiabao
 * @Project : reserver
 * @Time : 2021/11/6 11:54
 * @Desc : json字符串和对象之间相互转换
 */
public class JsonUtils {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String bean2Json(Object value) {
        if (value != null) {
            try {
                return objectMapper.writeValueAsString(value);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static <T> T json2bean(String json, Class<T> classType) {
        if (json != null) {
            try {
                return objectMapper.readValue(json, classType);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
